<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="file" name="" id="file" value="" />
		<button onclick="test()">异步测试</button>
	</body>
	<script src="js/spark-md5.min.js"></script>
	<script>
		function calculate(){
			var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
				fileInfo = document.getElementById("file").value
				chunkSize = 2097152, // Read in chunks of 2MB
				chunks = Math.ceil(fileInfo.size / chunkSize),
				currentChunk = 0,
				spark = new SparkMD5.ArrayBuffer(),
				fileReader = new FileReader();
			
			fileReader.onload = function(e) {
				console.log('read chunk nr', currentChunk + 1, 'of', chunks);
				spark.append(e.target.result); // Append array buffer
				currentChunk++;
			
				if (currentChunk < chunks) {
					loadNext();
				} else {
					console.log('finished loading');
					console.info('computed hash', spark.end()); // Compute hash
				}
			};
			
			fileReader.onerror = function() {
				console.warn('oops, something went wrong.');
			};
			
			function loadNext() {
				var start = currentChunk * chunkSize,
					end = ((start + chunkSize) >= fileInfo.size) ? fileInfo.size : start + chunkSize;
			
				fileReader.readAsArrayBuffer(blobSlice.call(fileInfo, start, end));
			}
			
			loadNext();
		}
		
		async function test(){
			var result = await calculate()
			console.log(result)
		}
	</script>
	
</html>
